package com.citycloud.ccuap.tc.yq.metatablecate;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.SelectProvider;

import com.citycloud.ccuap.commons.db.SqlRestrictions;
import com.citycloud.ccuap.tc.yq.entity.MetaField;
import com.citycloud.ccuap.tc.yq.entity.MetaTable;
import com.citycloud.ccuap.tc.yq.entity.MetaTableCate;

@Mapper
public interface TableCateMapper {

	@SelectProvider(type = SqlProvider.class, method = "findByDataGroupId")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaTableCate.ResultMapExt")
	List<MetaTableCate> findByDataGroupId(String groupId);
	
	@SelectProvider(type = SqlProvider.class, method = "findByDataGroupIdNew")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaField.ResultMapExt")
	List<MetaField> findByDataGroupIdNew(String groupId);
	
	@SelectProvider(type = SqlProvider.class, method = "findCateTableByGroupId")
	@ResultMap("com.citycloud.ccuap.tc.yq.entity.MetaTable.ResultMapExt")
	List<MetaTable> findCateTableByGroupId(String groupId);

	class SqlProvider {
		public String findByDataGroupId(String groupId) {
			StringBuffer sql = new StringBuffer(
					"SELECT * FROM ap_t_meta_table_cate_view as atm_tc LEFT JOIN sys_dimension_cls as ats_dc ON atm_tc.OBJ_ID = ats_dc.cate_id WHERE 1 =1 ");
			sql.append(SqlRestrictions.eq("ats_dc.DATAGROUP_ID", groupId));
			return sql.toString();
		}
		
		public String findByDataGroupIdNew(String groupId) {
			StringBuffer sql = new StringBuffer(
					"SELECT amf.* FROM ap_t_meta_field_view as amf INNER JOIN ap_t_meta_table_view as amt on amf.table_id=amt.obj_id INNER JOIN ap_t_meta_table_cate_view as atm_tc on amt.cate_id=atm_tc.obj_id LEFT JOIN sys_dimension_cls as ats_dc ON atm_tc.OBJ_ID = ats_dc.cate_id WHERE 1 =1" );
			sql.append(SqlRestrictions.eq("ats_dc.DATAGROUP_ID", groupId));
			return sql.toString();
		}
		
		public String findCateTableByGroupId(String groupId) {
			StringBuffer sql = new StringBuffer(
					"SELECT atm.* FROM ap_t_meta_table_view atm inner join ap_t_meta_table_cate_view as atm_tc  on atm_tc.obj_id=atm.cate_id LEFT JOIN sys_dimension_cls as ats_dc ON atm_tc.OBJ_ID = ats_dc.cate_id WHERE 1 =1 " );
			sql.append(SqlRestrictions.eq("ats_dc.DATAGROUP_ID", groupId));
			return sql.toString();
		}

	}

	
	
}
